#coding:utf8

import urllib
import urllib.request
import json
import logging

logger = logging.getLogger("curl")

def get(url, timeout=60, headers={}):
    logInfo = {'url': url, 'method': 'GET', 'headers': headers}

    try:
        req = urllib.request.Request(url, headers=headers, method='GET')
        response = urllib.request.urlopen(url=req, timeout=timeout)
        result = json.loads(response.read().decode())
        response.close()

        logInfo.update({'response_code': response.getcode(), 'response_body': result})
        logger.info(json.dumps(logInfo))

        return response.getcode() == 200, result
    except Exception as e:
        logInfo.update({'response_code': 500, 'response_err': str(e)})
        logger.error(json.dumps(logInfo))

        return False, None

def post(requrl, parms, timeout=60, headers={}):
    logInfo = {'url': requrl, 'method': 'POST', 'request_data': parms, 'headers': headers}

    try:
        data_urlencode = urllib.parse.urlencode(parms)
        req = urllib.request.Request(url = requrl, data = data_urlencode.encode('utf-8'), headers=headers)
        response = urllib.request.urlopen(req, timeout=timeout)
        result = json.loads(response.read())
        response.close()

        logInfo.update({'response_code': response.getcode(), 'response_body': result})
        logger.info(json.dumps(logInfo))

        return response.getcode() == 200, result
    except Exception as e:
        logInfo.update({'response_code': 500, 'response_err': str(e)})
        logger.error(json.dumps(logInfo))

        return False, None

def postJson(requrl, parms, timeout=60, headers={}):
    logInfo = {'url': requrl, 'method': 'JSON', 'request_data': parms, 'headers': headers}

    try:
        data_urlencode = json.dumps(parms, ensure_ascii=False)
        headers.update({'Content-Type': 'application/json'})
        req = urllib.request.Request(url=requrl, data=data_urlencode.encode('utf8'), headers=headers)
        response = urllib.request.urlopen(req, timeout=timeout)
        result = json.loads(response.read(), strict=False)
        response.close()

        logInfo.update({'response_code': response.getcode(), 'response_body': result})
        logger.info(json.dumps(logInfo))

        return response.getcode() == 200, result
    except Exception as e:
        logInfo.update({'response_code': 500, 'response_err': str(e)})
        logger.error(json.dumps(logInfo))

        return False, None

def postText(requrl, text, timeout=60):
    logInfo = {'url': requrl, 'method': 'POST_TEXT', 'request_text': text}

    try:
        req = urllib.request.Request(url = requrl, data = text.encode('utf-8'))
        response = urllib.request.urlopen(req, timeout=timeout)
        result = response.read().decode('utf-8')
        response.close()

        logInfo.update({'response_code': response.getcode(), 'response_text': result})
        logger.info(json.dumps(logInfo))

        return response.getcode() == 200, result
    except Exception as e:
        logInfo.update({'response_code': 500, 'response_err': str(e)})
        logger.error(json.dumps(logInfo))

        return False, str(e)

